﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Greve
{
    public partial class CupManager : Form
    {
        #region variables
        // variables

        public bool pressed;
        public List<Cup> cups;
        public Cup selectedCup;
        public SubCup selectedSubCup;
        public int jumpheight;

        List<String> stringList;
        List<JumpCode> codes;

        Dictionary<int,String> maleList;
		Dictionary<int,String> femaleList;

        NewJumper jumperDialog;

        // endofvariables
        #endregion


        #region Functions
        private void resetSubUI()
        {
            labelSelectedSubCupName.Text = "None";
            textBoxSubCupName.Text = "";
            comboBoxSubCup.Items.Clear();
            comboBoxSubCup.Text = "";

            
            // clear subCup stats
			uncheckProperties();
        }

		private void uncheckProperties()
		{
			uncheckGender();
			
			uncheckJumpHeight();
		}
        private void resetUI()
        {
            // clear cup stuffs
            comboBoxCupList.Items.Remove(labelSelectedCupName.Text);
            labelSelectedCupName.Text = "None";
            textBoxClub.Text = "";
            textBoxCupName.Text = "";
            comboBoxCupList.Text = "";

            comboBoxClass.Text = "";
            
            
            uncheckJudge();
            // clear subCup

            resetSubUI();

        }

        private void uncheckGender()
        {
            radioButtonGenderMale.Checked = false;
            radioButtonGenderWomen.Checked = false;
        }

        private void uncheckJudge()
        {
            radioButton3Judges.Checked = false;
            radioButton5Judges.Checked = false;
            radioButton7Judges.Checked = false;
            radioButton9Judges.Checked = false;
        }

        private void uncheckJumpHeight()
        {
            radioButton1Meter.Checked = false;
            radioButton3Meters.Checked = false;
            radioButtonHighJumps.Checked = false;
        }

        private void functionNewCup()
        {
            SubCup sc = new SubCup();
            Cup c = new Cup();
            selectedCup = c;
            selectedSubCup = sc;

            c.name = "New Cup";
            setJudges(5);
            cups.Add(c);
            comboBoxCupList.Items.Add(c.name);
            textBoxCupName.Text = c.name;
            textBoxClub.Text = "";
            labelSelectedCupName.Text = c.name;
            comboBoxCupList.SelectedItem = c.name;

        }

        private void cupNameChange()
        {
            if (selectedCup != null)
            {
                int i = comboBoxCupList.Items.IndexOf(selectedCup.name);

                
                selectedCup.name = textBoxCupName.Text;
                comboBoxCupList.Items[i] = selectedCup.name;
                labelSelectedCupName.Text = selectedCup.name;
            }
        }
        private void subCupNameChange()
        {
            if (selectedCup != null)
            {
                if (selectedSubCup != null)
                {
                    int i = comboBoxSubCup.Items.IndexOf(selectedSubCup.name);

                    selectedSubCup.name = textBoxSubCupName.Text;
                    comboBoxSubCup.Items[i] = selectedSubCup.name;
                    labelSelectedSubCupName.Text = selectedSubCup.name;
                }
            }
            else
                textBoxSubCupName.Text = "";
        }

        private void comboBoxCupListChangeSelected()
        {
            String index = comboBoxCupList.SelectedItem.ToString();
            foreach (Cup c in cups)
            {
                if (c.name.Equals(index))
                {
                    selectedCup = c;
                    break;
                }
            }
            textBoxCupName.Text = selectedCup.name;
            textBoxClub.Text = selectedCup.club;
            labelSelectedCupName.Text = selectedCup.name;
            if (selectedCup.getMaxJudges() != 0)
            {
                if (selectedCup.getMaxJudges() == 3)
                    radioButton3Judges.Checked = true;
                else if (selectedCup.getMaxJudges() == 5)
                    radioButton5Judges.Checked = true;
                else if (selectedCup.getMaxJudges() == 7)
                    radioButton7Judges.Checked = true;
                else
                    radioButton9Judges.Checked = true;
            }
        }

        private void setNewSubCupList()
        {
            comboBoxSubCup.Items.Clear();
            comboBoxSubCup.Text = "";
            selectedSubCup = null;
            textBoxSubCupName.Text = "";
            labelSelectedSubCupName.Text = "None";
            for (int i = 0; i < selectedCup.subCup.Count; i++)
                comboBoxSubCup.Items.Add(selectedCup.subCup[i].name);
        }

        private void functionRemoveCup()
        {
            cups.Remove(selectedCup);
            selectedCup = null;

            resetUI();
        }

        private void updateComboBoxClassSelected()
        {
            if (selectedCup != null)
            {
                selectedCup.subCup.Clear();
                comboBoxSubCup.Items.Clear();
                if (comboBoxClass.SelectedIndex.Equals(0))
                {
                    SubCup sub;

                    for(int i = 0 ; i < 6; i++)
                    {
                        #region subcupcreate
                        if (i == 0)
                        {
                            sub = new SubCup();
                            subCupCreate(sub,"1Meter Men", "Male", 6,1,5);
                        }
                        else if (i == 1)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "1Meter Women", "Female", 5, 1, 5);
                        }
                        else if (i == 2)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "3Meter Men", "Male", 6, 3, 5);
                        }
                        else if (i == 3)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "3Meter Women", "Female", 5, 3, 5);
                        }
                        else if (i == 4)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "High Dive Men", "Male", 6, 5, 5);

                        }
                        else if (i == 5)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "High Dive Women", "Female", 5, 5, 5);
                        }
                        #endregion
                    }
                }
				else if (comboBoxClass.SelectedIndex.Equals(1))
				{
					SubCup sub;
					for (int i = 0; i < 6; i++)
					{
						#region subcupcreate
						if (i == 0)
						{
                            sub = new SubCup();
                            subCupCreate(sub, "1Meter Men", "Male", 10, 1, 5);
						}
						else if (i == 1)
						{
                            sub = new SubCup();
                            subCupCreate(sub, "1Meter Women", "Female", 9, 1, 5);
						}
						else if (i == 2)
						{
                            sub = new SubCup();
                            subCupCreate(sub, "1Meter Men", "Male", 10, 3, 5);
						}
						else if (i == 3)
						{
                            sub = new SubCup();
                            subCupCreate(sub, "1Meter Women", "Female", 9, 3, 5);
						}
						else if (i == 4)
						{
                            sub = new SubCup();
                            subCupCreate(sub, "High Dive Men", "Male", 9, 5, 5);
						}
						else if (i == 5)
						{
                            sub = new SubCup();
                            subCupCreate(sub, "High Dive Men", "Male", 8, 5, 5);
						}
						#endregion
					}
				}
				else if (comboBoxClass.SelectedIndex.Equals(2))
				{
					SubCup sub;
					for (int i = 0; i < 6; i++)
					{
                        #region subcupcreate
                        if (i == 0)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "1Meter Men", "Male", 9, 1, 5);
                        }
                        else if (i == 1)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "1Meter Women", "Female", 8, 1, 5);
                        }
                        else if (i == 2)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "1Meter Men", "Male", 9, 3, 5);
                        }
                        else if (i == 3)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "1Meter Women", "Female", 8, 3, 5);
                        }
                        else if (i == 4)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "High Dive Men", "Male", 8, 5, 5);
                        }
                        else if (i == 5)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "High Dive Men", "Male", 7, 5, 5);
                        }
                        #endregion
						
					}
				}
				else if (comboBoxClass.SelectedIndex.Equals(3))
				{
					SubCup sub;
					for (int i = 0; i < 6; i++)
					{
                        #region subcupcreate
                        if (i == 0)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "1Meter Men", "Male", 8, 1, 5);
                        }
                        else if (i == 1)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "1Meter Women", "Female", 7, 1, 5);
                        }
                        else if (i == 2)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "1Meter Men", "Male", 8, 3, 5);
                        }
                        else if (i == 3)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "1Meter Women", "Female", 7, 3, 5);
                        }
                        else if (i == 4)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "High Dive Men", "Male", 7, 5, 5);
                        }
                        else if (i == 5)
                        {
                            sub = new SubCup();
                            subCupCreate(sub, "High Dive Men", "Male", 6, 5, 5);
                        }
                        #endregion
					}
				}
            }
        }

        private void subCupIndexChanged()
        {
            String index = comboBoxSubCup.SelectedItem.ToString();
            if (selectedCup != null)
            {
				for (int i = 0; i < selectedCup.subCup.Count(); i++)
				{
					if (selectedCup.subCup[i].name.Equals(index))
					{
						selectedSubCup = selectedCup.subCup[i];
						break;
					}
				}
                textBoxSubCupName.Text = selectedSubCup.name;
                labelSelectedSubCupName.Text = selectedSubCup.name;
                textBoxMaxJumps.Text = selectedSubCup.maxJumps.ToString();

            }
        }

        private void functionAddSubCup()
        {
			if (selectedCup != null)
			{
				selectedSubCup = new SubCup();
				selectedSubCup.name = "new Sub Cup";
				selectedCup.addSubCup(selectedSubCup);
				comboBoxSubCup.Items.Add(selectedSubCup.name);
				comboBoxSubCup.SelectedItem = selectedSubCup.name;
				textBoxSubCupName.Text = selectedSubCup.name;
				labelSelectedSubCupName.Text = selectedSubCup.name;
				radioButton1Meter.Checked = true;
				radioButtonGenderMale.Checked = true;
			}
			else
				MessageBox.Show("There is no cup", "Error");
        }
       
        private void newCupButton()
        {
            functionNewCup();
            setNewSubCupList();
            textBoxCupName.Focus();
            textBoxCupName.SelectAll();
            comboBoxClass.Text = "";
        }

        private void startCupButton()
        {
            CupRunning cr = new CupRunning(this);
			cr.start();
			cr.ShowDialog();
        }

        private void selectClub(KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Enter))
            {
                textBoxClub.Select();
                e.Handled = true;
                e.SuppressKeyPress = true;
            }
        }

        private void setGender(String gender)
        {
            if (selectedSubCup != null)
            {
                selectedSubCup.setSubCupGender(gender);
            }
            else
                uncheckGender();
        }

        private void SubCupRemove()
        {
            if (selectedCup != null && selectedSubCup != null)
            {
                selectedCup.removeSubCup(selectedSubCup);
                
                resetSubUI();
                setNewSubCupList();
                selectedSubCup = null;
            }
			else
				MessageBox.Show("No selected subcup to remove!", "Error");
        }

        private void setJudges(int judge)
        {
            if (selectedCup != null)
                selectedCup.setJudges(judge);
        }

        private void addJudge()
        {
            if (selectedCup != null)
            {
                if(selectedCup.judgeCount != selectedCup.maxJudges)
                {
                    Forms.AddJudge j = new Forms.AddJudge();
                    j.ShowDialog();
                    if (!j._cancel)
                    {
                        selectedCup.addJudge(j.judge);
                        listBoxJudges.Items.Add(j.judge._name);
                    }
                }
                else
                    MessageBox.Show("Maximum number of judges added!", "Error");
            }
        }
		private void removeJudge()
		{
			if(selectedCup != null)
			{
				if (listBoxJudges.SelectedIndex != -1)
				{
					selectedCup.judges.RemoveAt(listBoxJudges.SelectedIndex);
					listBoxJudges.Items.Remove(listBoxJumpers.SelectedItem);
					selectedCup.judgeCount--;
				}
				else
					MessageBox.Show("No judge to remove.", "Error!");
			}
		}

        private void addJumper()
        {
			if (selectedSubCup != null && selectedSubCup.maxJumps != 0 && selectedSubCup.maxHeight != 0)
			{
				if(selectedSubCup.subCupGender.Equals("Male"))
					jumperDialog = new NewJumper(selectedSubCup.maxJumps, selectedSubCup.subCupGender, selectedSubCup.maxHeight,maleList,codes);
				else
					jumperDialog = new NewJumper(selectedSubCup.maxJumps, selectedSubCup.subCupGender, selectedSubCup.maxHeight,femaleList,codes);
				jumperDialog.ShowDialog();
				if (!jumperDialog.cancel)
				{
					selectedSubCup.jumpers.Add(jumperDialog.p);
					listBoxJumpers.Items.Add(jumperDialog.p.name);
				}
			}
        }

        private void enableOverride()
        {
            if(checkBoxOverride.Checked)
                textBoxMaxJumps.Enabled = true;
            else
                textBoxMaxJumps.Enabled = false;
        }


        private void ChangeJumps(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) /*&&
       (e.KeyChar != '.')*/)
            {
                e.Handled = true;
            }

            if(selectedSubCup != null && !textBoxMaxJumps.Text.Equals(""))
            {
                selectedSubCup.maxJumps = int.Parse(textBoxMaxJumps.Text);
            }
        }

        private void MainWindowCheckKeyPress(object sender, KeyEventArgs e)
        {
            if(e.Control && e.Shift && e.KeyCode.Equals(Keys.A) && !pressed)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
                pressed = true;
                addJumper();
            }
            if(e.Control && e.Alt && e.KeyCode.Equals(Keys.A) && !pressed)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
                pressed = true;
                addJudge();
            }
            if(e.Control && e.Shift && e.KeyCode.Equals(Keys.Z) && !pressed)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
                pressed = true;
                removeDiver();
            }
            if (e.Control && e.Alt && e.KeyCode.Equals(Keys.Z) && !pressed)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
                pressed = true;
                removeJudge();
            }
			if (e.Control && e.Shift && e.KeyCode.Equals(Keys.N) && !pressed)
			{
				e.Handled = true;
				e.SuppressKeyPress = true;
				pressed = true;
				uncheckProperties();
				functionAddSubCup();
			}
			if (e.Control && e.Shift && e.KeyCode.Equals(Keys.D) && !pressed)
			{
				e.Handled = true;
				e.SuppressKeyPress = true;
				pressed = true;
				SubCupRemove();
			}
            
            if (e.Control && e.KeyCode.Equals(Keys.N) && !pressed)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
                pressed = true;
                newCupButton();
            }

            if (e.Control && e.KeyCode.Equals(Keys.D) && !pressed && selectedCup != null)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
                pressed = true;
                functionRemoveCup();
            }

            if (e.Control && e.KeyCode.Equals(Keys.L) && !pressed)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
                pressed = true;
                comboBoxCupList.Focus();
            }

            if (e.Control && e.KeyCode.Equals(Keys.R) && !pressed)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
                pressed = true;
                textBoxCupName.Focus();
                textBoxCupName.SelectAll();
            }

            if (e.Shift && e.Control && e.KeyCode.Equals(Keys.C) && !pressed)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
                pressed = true;
                textBoxClub.Focus();
                textBoxClub.SelectAll();
            }

            if (e.Alt && e.KeyCode.Equals(Keys.C) && !pressed)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
                pressed = true;
                comboBoxClass.Focus();
            }

            if (e.Control && e.Shift && e.KeyCode.Equals(Keys.S) && !pressed && selectedCup != null)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
                pressed = true;
                startCupButton();
            }

			if (e.Control && e.KeyCode.Equals(Keys.O) && !pressed)
			{
				e.Handled = true;
				e.SuppressKeyPress = true;
				pressed = true;
				if(!checkBoxOverride.Checked)
					checkBoxOverride.Checked = true;
				else
					checkBoxOverride.Checked = false;
			}
            
        }

        private void MainWindowCheckKeyUp(object sender, KeyEventArgs e)
        {
                e.Handled = true;
	            e.SuppressKeyPress = true;
                pressed = false;
        }

		private void setNewSubCup()
		{
			if (selectedSubCup != null)
			{
				if (selectedSubCup.maxHeight != 0)
				{
					if (selectedSubCup.maxHeight == 1)
						radioButton1Meter.Checked = true;
					else if (selectedSubCup.maxHeight == 3)
						radioButton3Meters.Checked = true;
					else
						radioButtonHighJumps.Checked = true;
				}
				if (selectedSubCup.subCupGender != null)
				{
					if (selectedSubCup.subCupGender.Equals("Male"))
						radioButtonGenderMale.Checked = true;
					else if (selectedSubCup.subCupGender.Equals("Female"))
						radioButtonGenderWomen.Checked = true;
				}
				
			}
		}

		private void setJumperList()
		{
			if (selectedSubCup != null)
			{
				listBoxJumpers.Items.Clear();
				foreach(Person p in selectedSubCup.jumpers)
					listBoxJumpers.Items.Add(p.name);
			}
		}

		private void setJudgeList()
		{
			if (selectedSubCup != null)
			{
				listBoxJudges.Items.Clear();
				foreach( Judge j in selectedCup.judges)
					if(j._name != null)
						listBoxJudges.Items.Add(j._name);
			}
		}

		private void removeDiver()
		{
			if (selectedSubCup != null && listBoxJumpers.SelectedIndex != -1)
			{
				if (listBoxJudges.SelectedIndex != -1)
				{
					selectedSubCup.jumpers.RemoveAt(listBoxJumpers.SelectedIndex);
					listBoxJumpers.Items.Remove(listBoxJumpers.SelectedItem);
				}
				else
					MessageBox.Show("No diver to remove.", "Error!");
			}
		}

        private void subCupCreate(SubCup sub,String name,String gender,int a ,int b,int c)
        {
            sub.name = name;
	        sub.subCupGender = gender;
            sub.maxJumps = a;
	        sub.maxHeight = b;
            selectedCup.addSubCup(sub);
            comboBoxSubCup.Items.Add(sub.name);
        }









        #endregion
        public CupManager()
        {
            this.KeyPreview = true;
            this.KeyDown += new KeyEventHandler(CupManager_KeyDown);



            maleList = new Dictionary<int,String>();
            femaleList = new Dictionary<int,String>();


            String a = "";
            String b = "";
            jumpheight = -1;
            cups = new List<Cup>();
            InitializeComponent();
            codes = new List<JumpCode>();
            stringList = Functions.readString();
            for (int i = 0; i < stringList.Count; i++ )
            {
                if (i % 5 == 0)
                    a = stringList[i];
                else
                {
                    b = stringList[i];
                    String[] sub = b.Split(' ');
                    char c = Convert.ToChar(sub[0]);
                    double d = 0;
                    double e = 0; 
                    double f = 0; 
                    double g = 0; 
                    double h = 0;
                    Double.TryParse(sub[1].ToString(), out d);
                    Double.TryParse(sub[2].ToString(), out e);
                    Double.TryParse(sub[3].ToString(), out f);
                    Double.TryParse(sub[4].ToString(), out g);
                    Double.TryParse(sub[5].ToString(), out h);

                        
                    JumpCode cc = new JumpCode(a,c,d,e,f,g,h);
					cc.calcString();
                    codes.Add(cc);
                }
            }
            newCupButton();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            updateComboBoxClassSelected();
        }

        private void radioButton5Judges_CheckedChanged(object sender, EventArgs e)
        {
            setJudges(5);
        }

        private void radioButton9Judges_CheckedChanged(object sender, EventArgs e)
        {
            setJudges(9);
        }

        private void newCup_Click(object sender, EventArgs e)
        {
            newCupButton();
        }
        private void textBoxCupName_TextChanged(object sender, EventArgs e)
        {
            cupNameChange();
        }

        private void comboBoxCupList_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxCupListChangeSelected();
            setNewSubCupList();
        }

        private void textBoxClub_TextChanged(object sender, EventArgs e)
        {
            if (selectedCup != null)
				selectedCup.club = textBoxClub.Text;
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
			if(selectedSubCup != null)
			{
				selectedSubCup.maxHeight = 1;
			}
        }

        private void radioButton3Meters_CheckedChanged(object sender, EventArgs e)
        {
			if (selectedSubCup != null)
			{
				selectedSubCup.maxHeight = 3;
			}
        }

        private void radioButtonHighJumps_CheckedChanged(object sender, EventArgs e)
        {
			if (selectedSubCup != null)
			{
				selectedSubCup.maxHeight = 5;
			}
        }

        private void textBoxSubCupName_TextChanged(object sender, EventArgs e)
        {
            subCupNameChange();
        }

        private void comboBoxSubCup_SelectedIndexChanged(object sender, EventArgs e)
        {
            subCupIndexChanged();
			setNewSubCup();
			setJumperList();
			setJudgeList();
        }

        private void RemoveCup_Click(object sender, EventArgs e)
        {
            functionRemoveCup();
        }

        private void buttonAddSubCup_Click(object sender, EventArgs e)
        {
			uncheckProperties();
            functionAddSubCup();
        }

        private void textBoxCupName_KeyDown(object sender, KeyEventArgs e)
        {
            selectClub(e);
        }

        private void buttonStartCup_Click(object sender, EventArgs e)
        {
            startCupButton();
        }

        private void radioButtonGenderMale_CheckedChanged(object sender, EventArgs e)
        {
            setGender("Male");
        }

        private void radioButtonGenderWomen_CheckedChanged(object sender, EventArgs e)
        {
			setGender("Female");
        }

        private void buttonRemoveSubCup_Click(object sender, EventArgs e)
        {
            SubCupRemove();
        }

        private void radioButton3Judges_CheckedChanged(object sender, EventArgs e)
        {
            setJudges(3);
        }

        private void radioButton7Judges_CheckedChanged(object sender, EventArgs e)
        {
            setJudges(7);
        }

        private void buttonAddJudge_Click(object sender, EventArgs e)
        {
            addJudge();
        }

        private void buttonAddJumper_Click(object sender, EventArgs e)
        {
            addJumper();
        }

        private void checkBoxOverride_CheckedChanged(object sender, EventArgs e)
        {
            enableOverride();
        }

        private void textBoxMaxJumps_KeyPress(object sender, KeyPressEventArgs e)
        {
            ChangeJumps(sender, e);
        }

        private void CupManager_KeyDown(object sender, KeyEventArgs e)
        {
            MainWindowCheckKeyPress(sender, e);
        }

        private void CupManager_KeyUp(object sender, KeyEventArgs e)
		{
			MainWindowCheckKeyUp(sender, e);
        }

		private void buttonRemoveJumper_Click(object sender, EventArgs e)
		{
			removeDiver();
		}

		private void buttonRemoveJudge_Click(object sender, EventArgs e)
		{
			removeJudge();
		}

		private void CupManager_Load(object sender, EventArgs e)
		{

		}


        public void AddJump_Click(NewJumper j, EventArgs e)
        {
            //FAddJump();
        }
    }
}